package com.artisan.service.admin.impl;

import com.artisan.common.pojo.PageBean;
import com.artisan.common.pojo.PageParam;
import com.artisan.common.utils.CardUtils;
import com.artisan.common.utils.ListUtils;
import com.artisan.dao.BusinessMapper;
import com.artisan.medium.admin.service.BusinessService;
import com.artisan.model.Business;

import com.artisan.model.BusinessExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/7/1.
 */
@Service("businessService")
public class BusinessServiceImpl  implements BusinessService{
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public List<Business> getBusniessByPid(int pid) {
        BusinessExample businessExample=new BusinessExample();
        BusinessExample.Criteria criteria=businessExample.createCriteria();
        criteria.andPidEqualTo(pid);
        return businessMapper.selectByExample(businessExample);
    }

    @Override
    public PageBean listPage(PageParam pageParam, Map<String, Object> paramMap) {
        paramMap.put("pageFirst", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage());
        paramMap.put("pageSize", pageParam.getNumPerPage());
        paramMap.put("startRowNum", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage());
        paramMap.put("endRowNum", pageParam.getPageNum() * pageParam.getNumPerPage());
        // 统计总记录数
        Long count = businessMapper.listPageCount(paramMap);
        // 获取分页数据集
        List<Object> list = businessMapper.listPage(paramMap);
        // 构造分页对象
        return new PageBean(pageParam.getPageNum(), pageParam.getNumPerPage(), count.intValue(), list);
    }

    @Override
    public int insert(Business business) {
        return businessMapper.insert(business);
    }

    @Override
    public Business getBusinessById(int id) {

        return businessMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Business business) {
        return businessMapper.updateByPrimaryKey(business);
    }

    @Override
    public int delBusiness(String ids) {
        BusinessExample businessExample=new BusinessExample();
        BusinessExample.Criteria criteria=businessExample.createCriteria();
        criteria.andIdIn(ListUtils.StringToIntegerLst(CardUtils.INSTANCE.idstoList(ids)));
        return businessMapper.deleteByExample(businessExample);
    }

    @Override
    public List<Business> getBusinessByPid(Integer pid) {
        BusinessExample example = new BusinessExample();
        BusinessExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(pid);

        return businessMapper.selectByExample(example);
    }
}
